/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import Beans.Message;
import datas.MessageMapper;
import java.util.List;
import org.apache.ibatis.session.SqlSession;

/**

 @author Marie-Claude
 */
public class MessageDao {

  public static List<Message> getListMessageById(int userId) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      MessageMapper mapper = session.getMapper(MessageMapper.class);
      List<Message> lstmsgs = mapper.getAllMessages(userId);

      return lstmsgs;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static Message getMessageById(int messageId) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      MessageMapper mapper = session.getMapper(MessageMapper.class);
      Message msg = mapper.getMessageById(messageId);

      return msg;
    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static void insertMessage(Message message) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      MessageMapper mapper = session.getMapper(MessageMapper.class);
      mapper.insertMessage(message);
      session.commit();

    } finally {
      MyBatisManager.closeConnexion();
    }
  }

  public static void delete(int messageId) {
    SqlSession session = MyBatisManager.getSqlSession();

    try {

      MessageMapper mapper = session.getMapper(MessageMapper.class);
      mapper.delete(messageId);
      session.commit();

    } finally {
      MyBatisManager.closeConnexion();
    }
  }
}
